package router

import (
	"Last/controller"
	"github.com/gin-gonic/gin"
	"net/http"
)

/*
	路由相关文件
   @author:zjy
   @Emial:734219310@qq.com
*/

func InitRouter() *gin.Engine {
	//使用gin的默认配置
	gin := gin.Default()
	//使用跨域中间件
	gin.Use(Cors())

	LoginAbout := gin.Group("api")
	{
		LoginAbout.POST("/login", controller.LoginUser)       //等录
		LoginAbout.POST("/register", controller.RegisterUser) //注册
		LoginAbout.GET("/logout", controller.Logout)          //退出
	}
	UserInfoGroup := gin.Group("api/personal") //个人信息相关
	{
		UserInfoGroup.GET("/userinfo", controller.PersonalInfo) //用户信息相关
	}
	DateGroup := gin.Group("api/data")
	{
		DateGroup.POST("/dectitle", controller.Dectitle) //申报课题
		DateGroup.GET("/titleAll", controller.TitleAll)  //查询全部自身相关课题
		//上面是新写的
		DateGroup.GET("/list-part", controller.ListPart)
		//xx := shopGroup.Group("xx") 路由里面在嵌套路由
		DateGroup.GET("/search-goods", controller.SearchGoods)
		DateGroup.POST("/insert-goods", controller.InsertGoods)
		DateGroup.PUT("/update-goods", controller.UpdateGoods)
		//DateGroup.DELETE("/delete-goods", controller.DeleteGoods)
	}
	MenuTree := gin.Group("api/menu-tree")
	{
		MenuTree.GET("/all", controller.CreateMneuTree)
		//xx := shopGroup.Group("xx") 路由里面在嵌套路由
	}
	EchartDATA := gin.Group("api/EchartData")
	{
		EchartDATA.GET("one", controller.EchartOne)
		EchartDATA.GET("two", controller.EchartTwo)
		EchartDATA.GET("map", controller.EchartMap)
	}
	gin.NoRoute(controller.NoRouter)
	return gin
}

// 跨域的配置
func Cors() gin.HandlerFunc {
	return func(c *gin.Context) {
		method := c.Request.Method
		c.Header("Access-Control-Allow-Origin", "*")
		c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token")
		c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, PATCH, DELETE")
		c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
		c.Header("Access-Control-Allow-Credentials", "true")
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		c.Next()
	}
}
